package com.ytjj.qmyx.mall.service.h5.impl;

import com.github.pagehelper.PageHelper;
import com.ytjj.common.exception.ApiException;
import com.ytjj.common.utils.TokenUtil;
import com.ytjj.qmyx.mall.enums.ActivityTypeEnums;
import com.ytjj.qmyx.mall.mapper.ProductDao;
import com.ytjj.qmyx.mall.model.Product;
import com.ytjj.qmyx.mall.model.UsersFake;
import com.ytjj.qmyx.mall.model.dto.OldForNewProductDto;
import com.ytjj.qmyx.mall.model.response.ActivityProductResponse;
import com.ytjj.qmyx.mall.model.response.H5SkillPageResponse;
import com.ytjj.qmyx.mall.model.response.ProductShortResponse;
import com.ytjj.qmyx.mall.service.h5.H5SkillService;
import com.ytjj.qmyx.mall.utils.FakeUserUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 *
 *  *
 **/
@Service
public class H5SkillServiceImpl implements H5SkillService {

    @Resource
    private ProductDao productDao;

    @Resource
    private FakeUserUtil fakeUserUtil;

    @Override
    public H5SkillPageResponse getSkillPage(Integer pageNum, Integer pageSize) {
        H5SkillPageResponse h5SkillPageResponse = new H5SkillPageResponse();
        List<Integer> hostProductIdList=new ArrayList<>();
        List<ActivityProductResponse> hostList=productDao.findActivityHostList("SECKILL",3);
        hostList=hostList.stream().map(item -> {
            hostProductIdList.add(item.getId());

            try {
                //有设置秒杀占比
                if(item.getActivityMin().intValue()>0&&item.getActivityMin().intValue()<100){
                    item.setStockProgress(item.getActivityMin().intValue());
                    item.setStockProgressStr(item.getActivityMin().intValue() + "%");
                }else {
                    BigDecimal baseStock = new BigDecimal(Optional.ofNullable(item.getStock() % 17).orElse(0))
                            .multiply(new BigDecimal(100))
                            .divide(new BigDecimal(5000), 2, BigDecimal.ROUND_HALF_UP);
                    BigDecimal stockProgress = new BigDecimal(1)
                            .subtract(baseStock).setScale(2, BigDecimal.ROUND_DOWN).multiply(new BigDecimal(100));
                    item.setStockProgress(stockProgress.intValue());
                    item.setStockProgressStr(stockProgress.intValue() + "%");
                }
            } catch (ApiException e) {
                item.setStockProgress(80);
                item.setStockProgressStr("80%");
            }
            return item;
        })
                .collect(Collectors.toList());
        h5SkillPageResponse.setHostList(hostList);
        if(1==pageNum){//第一页需要取pageSize+3条
            pageSize+=3;
        }
        PageHelper.startPage(pageNum,pageSize);
        List<ActivityProductResponse> productList=productDao.findActivityList("SECKILL",null, null, TokenUtil.getPlatformData());
        productList=productList.stream().filter(item -> !hostProductIdList.contains(item.getId())).map(item -> {
            try {
                //有设置秒杀占比
                if(item.getActivityMin().intValue()>0&&item.getActivityMin().intValue()<100){
                    item.setStockProgress(item.getActivityMin().intValue());
                    item.setStockProgressStr(item.getActivityMin().intValue() + "%");
                }else {
                    BigDecimal baseStock = new BigDecimal(Optional.ofNullable(item.getStock() % 17).orElse(0))
                            .multiply(new BigDecimal(100))
                            .divide(new BigDecimal(5000), 2, BigDecimal.ROUND_HALF_UP);
                    BigDecimal stockProgress = new BigDecimal(1)
                            .subtract(baseStock).setScale(2, BigDecimal.ROUND_DOWN).multiply(new BigDecimal(100));
                    if (stockProgress.intValue() >= 100 || stockProgress.intValue() <= 0) {
                        item.setStockProgress(60);
                        item.setStockProgressStr("60%");
                    } else {
                        item.setStockProgress(stockProgress.intValue());
                        item.setStockProgressStr(stockProgress.intValue() + "%");
                    }
                }
            } catch (ApiException e) {
                item.setStockProgress(80);
                item.setStockProgressStr("80%");
            }
            return item;
        })
                .collect(Collectors.toList());
        if (productList.size() > 10) {
            List<ActivityProductResponse> top10ProductList = productList.subList(0, 10);
            List<ActivityProductResponse> lastProductList = productList.subList(10, productList.size());
            Collections.shuffle(top10ProductList);
            Collections.shuffle(lastProductList);
            productList = new ArrayList<>();
            productList.addAll(top10ProductList);
            productList.addAll(lastProductList);
        } else {
            Collections.shuffle(productList);
        }
        h5SkillPageResponse.setProductList(productList);
        // 假用户
        List<UsersFake> fakeUserList = fakeUserUtil.getFakeUserList();
        h5SkillPageResponse.setFakeUserList(fakeUserList);
        return h5SkillPageResponse;
    }

    @Override
    public List<ActivityProductResponse> getSkillList(Integer columnId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Product> productList = productDao.queryActivityProductList(ActivityTypeEnums.SECKILL.name(),columnId,null,null,null,TokenUtil.getPlatformData(),null,null,null,null);
        List<ActivityProductResponse> activityProductList = productList.stream()
                .map(item -> {
                    ActivityProductResponse activityProductResponse = new ActivityProductResponse(item);
                    return activityProductResponse;
                })
                .collect(Collectors.toList());
        activityProductList=activityProductList.stream().map(item -> {
            try {
                //有设置秒杀占比
                item.setActivityMin(Optional.ofNullable(item.getActivityMin()).orElse(BigDecimal.ZERO));
                if(item.getActivityMin().intValue()>0&&item.getActivityMin().intValue()<100){
                    item.setStockProgress(item.getActivityMin().intValue());
                    item.setStockProgressStr(item.getActivityMin().intValue() + "%");
                }else {
                    BigDecimal baseStock = new BigDecimal(Optional.ofNullable(item.getStock() % 17).orElse(0))
                            .multiply(new BigDecimal(100))
                            .divide(new BigDecimal(5000), 2, BigDecimal.ROUND_HALF_UP);
                    BigDecimal stockProgress = new BigDecimal(1)
                            .subtract(baseStock).setScale(2, BigDecimal.ROUND_DOWN).multiply(new BigDecimal(100));
                    if (stockProgress.intValue() >= 100 || stockProgress.intValue() <= 0) {
                        item.setStockProgress(60);
                        item.setStockProgressStr("60%");
                    } else {
                        item.setStockProgress(stockProgress.intValue());
                        item.setStockProgressStr(stockProgress.intValue() + "%");
                    }
                }
            } catch (ApiException e) {
                item.setStockProgress(80);
                item.setStockProgressStr("80%");
            }
            return item;
        }).collect(Collectors.toList());
        return activityProductList;
    }
}
